package com.fanrenqiji.modules.sys.web;

import com.fanrenqiji.common.config.GlobalSetting;
import com.fanrenqiji.common.persistence.Page;
import com.fanrenqiji.common.web.BaseController;
import com.fanrenqiji.modules.sys.entity.BlockList;
import com.fanrenqiji.modules.sys.service.BlockListService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping(value = "${adminPath}/sys/blockList")
public class BlockListController extends BaseController {
	@Autowired
	private BlockListService blockListService;

	@ModelAttribute("blockList")
	public BlockList getBlockList(@RequestParam(required = false) Long id) {
		if (id != null) {
			return blockListService.findOne(id);
		}
		return new BlockList();
	}

	@RequiresPermissions("sys:blockList:view")
	@RequestMapping(value = { "list", "" })
	public String list(BlockList blockList, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		Page<BlockList> page = blockListService.find(new Page<BlockList>(
				request, response), blockList);
		model.addAttribute("page", page);
		return "modules/sys/blockList";
	}

	@RequiresPermissions("sys:blockList:view")
	@RequestMapping(value = "form")
	public String form(Model model, BlockList blockList) {
		model.addAttribute("blockList", blockList);
		return "modules/sys/blockForm";
	}

	@RequiresPermissions("sys:blockList:add")
	@RequestMapping(value = "save")
	public String save(BlockList blockList, Model model,
			RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, blockList)) {
			return form(model, blockList);
		}
		blockListService.save(blockList);
		addMessage(redirectAttributes, "黑名单添加成功");
		return "redirect:" + GlobalSetting.getAdminPath() + "/sys/blockList/";
	}

	@RequiresPermissions("sys:blockList:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		blockListService.delete(id);
		addMessage(redirectAttributes, "黑名单删除成功");
		return "redirect:" + GlobalSetting.getAdminPath() + "/sys/blockList/";
	}

	@ResponseBody
	@RequestMapping(value = "checkMobile")
	public String checkMobile(String oldMobile, String mobile) {
		if (mobile != null && mobile.equals(oldMobile)) {
			return "true";
		}
		return blockListService.checkMobileOrIdCard(mobile);
	}

	@ResponseBody
	@RequestMapping(value = "checkIdCard")
	public String checkIdCard(String oldIdCard, String idCard) {
		if (idCard != null && idCard.equals(oldIdCard)) {
			return "true";
		}
		return blockListService.checkMobileOrIdCard(idCard);
	}
}
